const state = {
  cartList: []
}

const getters = {
  cartList(state) {
    return state.cartList
  },
  cartCount(state, getters) {
    return getters.cartList.length
  }
}
  
const mutations = {
  addCart(state, info) {
    // 1.查看是否添加过
    const oldInfo = state.cartList.find(item => item.id === info.id)
    // 2.+1或者新添加
    if (oldInfo) {
      oldInfo.count += 1
    } else {
      info.count = 1
      info.checked = true
      state.cartList.push(info)
    }
  }
}

const actions = {
    asyncAdd({commit}){          
      setTimeout(()=>{           
        commit('add')
      },1000)
    }
}

export default {
    namespaced: true,//需要加module 名称
    state,
    getters,
    mutations,
    actions,
}